﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using S7.Net;


namespace S7.NET
{
    public partial class Form1 : Form
    {
        public static string ipaddr="6.6.6.30";
        public static CpuType plctype = CpuType.S71200;
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 打开plc连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text=S7cs.openplc(plctype,ipaddr).ToString();
            timer1.Enabled=true;
             
        }

        /// <summary>
        /// 关闭PLC连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            label1.Text = S7cs.closeplc().ToString();
            timer1.Enabled=false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                label2.Text = S7cs.read(textBox1.Text);
            }
            catch (Exception)
            {

                label1.Text = "plc未连接"; ;
            }
       
        }
        /// <summary>
        /// 更改IP地址
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBoxPlcIP_TextChanged(object sender, EventArgs e)
        {
            ipaddr=textBoxPlcIP.Text;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 1; //设定下拉框的默认值
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;//DropDownList 下拉框文本不可编辑
                                                                 //DropDown 下拉框文本可编辑
        }

        private void timer1PLC心跳_Tick(object sender, EventArgs e)
        {
            心跳.Text = S7cs.read("M0.5");
        }

        /// <summary>
        /// 更改PLC类型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (comboBox1.SelectedItem)
            {
                case 1:
                    plctype = CpuType.S71200;
                    break;
                case 0:
                    plctype = CpuType.S71500;
                    break;
            }
            MessageBox.Show(plctype.ToString());
           
        }
    }
}
